<?php
/*
 * @var $model Menu
 */
cs()->registerScriptFile(Yii::app()->theme->baseUrl . '/js/jquery.validate.js');
?>

<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
	'id' => 'menu-form',
	'enableAjaxValidation' => false,
	'enableClientValidation' => false,
	'clientOptions' => array('validateOnSubmit' => true, 'validateOnType' => true)
    ));
    $model->old_parent_id = $model->parent_id;
    echo $form->hiddenField($model, 'param', array('id' => 'Menu_param'));
    echo $form->hiddenField($model, 'old_parent_id');
    ?>

    <div class="cleft">
        <table class="form" style="width: 100%">
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'extension_id'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'extension_id', array('' => t('MSG_FIRST_ITEM_TYPE', 'messages')) + Menu::model()->getExtensionIds(), array('style' => 'width:156px', 'id' => 'Menu_extension_id')); ?>
		    <?php echo $form->error($model, 'extension_id'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'title'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->textField($model, 'title', array('style' => 'width:255px')); ?>
		    <?php echo $form->error($model, 'title'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'alias'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->textField($model, 'alias', array('style' => 'width:255px')); ?>
		    <?php echo $form->error($model, 'alias'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->label($model, 'link'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->textField($model, 'link', array('style' => 'width:255px; font-size:80%', 'readonly' => 'readonly', 'id' => 'Menu_link')); ?>
		    <?php //echo $form->error($model, 'link'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'status'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'status', Menu::model()->getStatuses(), array('style' => 'width:156px')); ?>
		    <?php echo $form->error($model, 'status'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'access'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'access', Menu::model()->getAccesses(), array('style' => 'width:156px')); ?>
		    <?php echo $form->error($model, 'access'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'menu_type_id'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'menu_type_id', array('' => t('MSG_FIRST_ITEM_MENU', 'messages')) + Menu::model()->getMenus(), array('style' => 'width:156px', 'onchange' => 'loadParentID(' . $model->id . ');')); ?>
		    <?php echo $form->error($model, 'menu_type_id'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'parent_id'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'parent_id', array('0' => t('MSG_NO_PARENT', 'messages')) + Menu::model()->getParentId($model->id, $model->menu_type_id), array('style' => 'width:156px')); ?>
		    <?php echo $form->error($model, 'parent_id'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'target_window'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'target_window', Menu::model()->getTargetWindows(), array('style' => 'width:156px')); ?>
		    <?php echo $form->error($model, 'target_window'); ?>
                </td>
            </tr>
            <tr>
                <td class="label required"><?php echo $form->labelEx($model, 'language'); ?></td>
                <td class="input" width="298px">
		    <?php echo $form->dropDownList($model, 'language', array('*' => t('MSG_ALL_LANGUAGE', 'messages')) + CHtml::listData(Language::model()->findAll(), 'code', 'value'), array('style' => 'width:156px')); ?>
		    <?php echo $form->error($model, 'language'); ?>
                </td>
            </tr>
        </table>
    </div>
    <div class="cright">
        <fieldset>
            <legend><?php echo t('REQUIRED_SETTINGS', 'menu') ?></legend>
            <table id="tbl-settings">
                <tr name="article" style="display:none">
                    <td class="label required"><?php echo "<label class=\"required\" >" . t('LBL_SELECT_ARTICLE', 'menu') . " <span class=\"required\">*</span></label>"; ?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'article', array('size' => 20, 'maxlength' => 255, 'readonly' => 'readonly', 'id' => 'menu-article')); ?>
			<?php echo $form->error($model, 'article'); ?>
			<?php
			echo CHtml::button('::::', array(
			    'class' => 'update-dialog-open-link',
			    'dialog-width' => '750',
			    'dialog-height' => '500',
			    'href' => url('menu/selectarticle'),
			    'data-update-dialog-title' => t('LBL_SELECT_ARTICLE', 'menu')
			))
			?>
                    </td>
                </tr>
                <tr name="blog" style="display:none">
                    <td class="label required"><?php
			echo "<label class=\"required\" >" . t('LBL_SELECT_CATEGORY', 'menu') . " <span class=\"required\">*</span></label>";
			;
			?></td>
                    <td class="input" width="298px">
			<?php echo $form->dropDownList($model, 'categoryId', Category::model()->getListDataForParentDropDownList(), array('empty' => t('MSG_FIRST_CATEGORY', 'messages'), 'style' => 'width:180px', 'id' => 'Menu_category_id')); ?>
			<?php echo $form->error($model, 'categoryId'); ?>
                    </td>
                </tr>
		<tr name="gallery-item" style="display:none">
                    <td class="label required"><?php
			echo "<label class=\"required\" >" . t('LBL_SELECT_GALLERY', 'menu') . " <span class=\"required\">*</span></label>";
			;
			?></td>
                    <td class="input" width="298px">
			<?php echo $form->dropDownList($model, 'album_id', CHtml::listData(Album::model()->getListDataDropDownList(), 'id', 'name'), array('empty' => t('MSG_FIRST_GALLERY_ITEM', 'messages'), 'style' => 'width:180px', 'id' => 'Menu_gallery_item')); ?>
			<?php echo $form->error($model, 'album_id'); ?>
                    </td>
                </tr>
                <tr name="link" style="display:none">
                    <td class="label required"><?php
			echo "<label class=\"required\" >" . t('LBL_EXTERNAL_LINK', 'menu') . " <span class=\"required\">*</span></label>";
			;
			?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'external_link', array('style' => 'width:180px', 'id' => 'Menu_external_link')); ?>
			<?php echo $form->error($model, 'external_link'); ?>
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <legend><?php echo t('PAGE_DISPLAY_OPTIONS', 'menu') ?></legend>
            <table>
                <tr>
                    <td class="label required"><?php echo $form->labelEx($model, 'page_title'); ?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'page_title', array('style' => 'width:255px')); ?>
			<?php echo $form->error($model, 'page_title'); ?>
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <legend><?php echo t('METADATA_OPTIONS', 'menu') ?></legend>
            <table>
                <tr>
                    <td class="label required"><?php echo $form->labelEx($model, 'meta_desc'); ?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'meta_desc', array('style' => 'width:255px')); ?>
			<?php echo $form->error($model, 'meta_desc'); ?>
                    </td>
                </tr>
                <tr>
                    <td class="label required"><?php echo $form->labelEx($model, 'meta_keyword'); ?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'meta_keyword', array('style' => 'width:255px')); ?>
			<?php echo $form->error($model, 'meta_keyword'); ?>
                    </td>
                </tr>
                <tr>
                    <td class="label required"><?php echo $form->labelEx($model, 'robots'); ?></td>
                    <td class="input">
			<?php echo $form->dropDownList($model, 'robots', Menu::model()->getRobots(), array('empty' => t('MSG_FIRST_ROBOTS', 'messages'), 'style' => 'width:269px')); ?>
			<?php echo $form->error($model, 'robots'); ?>
                    </td>
                </tr>
            </table>
        </fieldset>
        <fieldset>
            <legend><?php echo t('LINK_TYPE_OPTIONS', 'menu') ?></legend>
            <table>
                <tr>
                    <td class="label required"><?php echo $form->labelEx($model, 'css_style'); ?></td>
                    <td class="input" width="298px">
			<?php echo $form->textField($model, 'css_style'); ?>
			<?php echo $form->error($model, 'css_style'); ?>
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    <br clear="both" />
    <?php $this->endWidget(); ?>
</div>

<script src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.tooltip.js" type="text/javascript"></script>
<script type="text/javascript">
    LGCMS.addTooltip();
    var link = '';

    $(document).ready(function() {
	declareEvents();
	handleSettings();
    });

    function declareEvents() {
	$('#Menu_extension_id').change(function() {
	    // Clear values
	    $('#Menu_param').val('');
	    $('#Menu_category_id').val('');
	    $('#menu-article').val('');
	    $('#Menu_external_link').val('');
	    $('#Menu_gallery_item').val('');
	    handleSettings();
	});

	$('#Menu_category_id').change(function() {
	    setRequiredId($(this).val());
	    //$('#Menu_param').val('');
	});

	$('#Menu_gallery_item').change(function() {
	    setRequiredId($(this).val());
	    //$('#Menu_param').val('');
	});

	$('#Menu_external_link').change(function() {
	    $('#Menu_link').val($(this).val());

	});

	$('#Menu_alias').change(function() {
	    var n = $('#Menu_link').val().split("/");
	    var aliasString = "";

	    if ($("#Menu_alias").val() !== "") {
		aliasString = "-" + $("#Menu_alias").val();
	    }

	    $('#Menu_link').val(n[0] + '/' + n[1] + "/" + (n[2].split("-"))[0] + aliasString);
	});


	$("#menu-form").validate({
	    debug: false,
	    errorElement: "span",
	    errorPlacement: function(error, element) {
		error.tooltip({
		    bodyHandler: function() {
			return error.html();
		    }
		});
		error.insertAfter(element);
	    }
	});
    }

    function handleSettings() {
	$('#tbl-settings').find('tr').hide();
	var data = getChangeExtension($('#Menu_extension_id').val());
	if (data) {
	    link = data['link'];
	    $('#Menu_link').val(link);
	    $("#menu-article").rules("remove");
	    $("#Menu_category_id").rules("remove");
	    $("#Menu_gallery_item").rules("remove");
	    $("#Menu_external_link").rules("remove");

	    var view = data['view'];
	    if (view == "<?php echo LGConstant::EXTENSION_VIEW_SINGLE_ARTICLE ?>") {
		$('#tbl-settings').find('tr[name=article]').show();
		//$('#menu-article').rules("add", {required: true});
	    } else if (view == "<?php echo LGConstant::EXTENSION_VIEW_CATEGORY_BLOG ?>") {
		$('#tbl-settings').find('tr[name=blog]').show();
		//$('#Menu_category_id').rules("add", {required: true});
	    } else if (view == "<?php echo LGConstant::EXTENSION_VIEW_EXTERNAL_LINK ?>") {
		$('#tbl-settings').find('tr[name=link]').show();
		$('#Menu_external_link').change();
		//$('#Menu_external_link').rules("add", {required: true});
	    } else if (view == "<?php echo LGConstant::EXTENSION_VIEW_GALLERY_ITEM ?>") {
		$('#tbl-settings').find('tr[name=gallery-item]').show();
		$('#Menu_gallery_item').change();
		//$('#Menu_gallery_item').rules("add", {required: true});
	    }
	}
    }

    function setRequiredId(id) {
	var n = link.split("/");
	if (id == '') {
	    $('#Menu_param').val('');
	    $('#Menu_link').val(link);
	} else {
	    $('#Menu_param').val('{"id":"' + id + '"}');

	    var url = ''
	    if ($("#Menu_alias").val() != null && $("#Menu_alias").val() != "") {
		url = '-' + $("#Menu_alias").val();
	    }
	    $('#Menu_link').val(n[0] + '/' + n[1] + "/" + id + url);
	}
    }

    function getChangeExtension(id) {
	var result = '';
	$.ajax({
	    type: "GET",
	    dataType: "text",
	    url: "<?php echo url('menu/getExtensionView') ?>",
	    async: false,
	    data: ({
		'id': id,
		'param': $('#Menu_param').val(),
		'alias': '-' + $("#Menu_alias").val()
	    }),
	    success: function(data) {
		result = $.parseJSON(data);
	    }
	});

	return result;
    }


    function loadParentID(menuID) {
	var id = $('#Menu_menu_type_id option:selected').val();

	if (id === '') {
	    $('#Menu_parent_id').html('');
	    return;
	}
	//alert($('#' + 'Menu_parent_id').html());
	$.ajax(
		{
		    // The link we are accessing.
		    url: '<?php echo url($this->id . '/' . 'loadParentID') ?>',
		    // The type of request.
		    data: 'id=' + menuID + '&menuTypeID=' + id,
		    type: "get",
		    // The type of data that is getting returned.
		    dataType: "html",
		    error: function() {
			$("#Menu_menu_type_id option[value='']").attr("selected", "selected");
			alert('Have an error. Please select Menu again!');
		    },
		    beforeSend: function() {
			//ShowStatus("AJAX - beforeSend()");
			LGCMS.loading();
		    },
		    complete: function() {
			//ShowStatus("AJAX - complete()");
			LGCMS.completed();
		    },
		    success: function(strData) {
			var myOptions = $.parseJSON(strData);
			//ShowStatus("AJAX - success()");
			$("#Menu_parent_id").fadeOut(0);
			$('#Menu_parent_id').html('');

			$.each(myOptions, function(val, text) {
			    $('#Menu_parent_id').append($('<option></option>').val(val).html(text));
			});
			$("#Menu_parent_id").fadeIn('fast');
		    }
		}
	);
    }
    ;
</script>

<style type="text/css">
    .cleft { float: left; width: 539px; }
    .cright { float: right; width: 500px; }
</style>